package com.zts.util;

import java.util.List;
import java.util.Map;
import java.util.Set;
import javax.validation.*;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.zts.base.exception.UnprocessableEntityException;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * Created by lily on 2017/6/7.
 */

/**
 * JSR303 Validator(Hibernate Validator)工具类.
 * 提供给 service层使用。
 * controller 层建议使用 spring注解的方式。
 */
public class BeanValidatorsUtil {

    @Autowired
    protected static Validator validator;

    public BeanValidatorsUtil() {
        if (null == validator) {
             /* 创建效验工厂 */
            ValidatorFactory validatorFactory = Validation.buildDefaultValidatorFactory();
            validator = validatorFactory.getValidator();
        }
    }

    /**
     * 调用JSR303的validate方法, 验证失败时抛出ConstraintViolationException.
     */
    public static void validateWithException(Validator validator, Object object, Class<?>... groups)
            throws UnprocessableEntityException {
        Set<? extends ConstraintViolation<?>> constraintViolations = validator.validate(object, groups);
        if (!constraintViolations.isEmpty()) {
            //输出异常提示信息 zhangkh 2017-02-16
            List<String> message = extractMessage(constraintViolations);
            throw new UnprocessableEntityException(StringUtils.join(message.toArray(), ';'),
                    new ConstraintViolationException(constraintViolations));
        }
    }

    public static void validateWithException(Object object)
            throws UnprocessableEntityException {
        ValidatorFactory validatorFactory = Validation.buildDefaultValidatorFactory();
        validator = validatorFactory.getValidator();
        Set<? extends ConstraintViolation<?>> constraintViolations = validator.validate(object);
        if (!constraintViolations.isEmpty()) {
            //输出异常提示信息 zhangkh 2017-02-16
            List<String> message = extractMessage(constraintViolations);
            throw new UnprocessableEntityException(StringUtils.join(message.toArray(), ';'),
                    new ConstraintViolationException(constraintViolations));
        }
    }

    /**
     * 辅助方法, 转换ConstraintViolationException中的Set<ConstraintViolations>中为List<message>.
     */
    public static List<String> extractMessage(ConstraintViolationException e) {
        return extractMessage(e.getConstraintViolations());
    }

    /**
     * 辅助方法, 转换Set<ConstraintViolation>为List<message>
     */
    public static List<String> extractMessage(Set<? extends ConstraintViolation> constraintViolations) {
        List<String> errorMessages = Lists.newArrayList();
        for (ConstraintViolation violation : constraintViolations) {
            errorMessages.add(violation.getMessage());
        }
        return errorMessages;
    }

    /**
     * 辅助方法, 转换ConstraintViolationException中的Set<ConstraintViolations>为Map<property, message>.
     */
    public static Map<String, String> extractPropertyAndMessage(ConstraintViolationException e) {
        return extractPropertyAndMessage(e.getConstraintViolations());
    }

    /**
     * 辅助方法, 转换Set<ConstraintViolation>为Map<property, message>.
     */
    public static Map<String, String> extractPropertyAndMessage(Set<? extends ConstraintViolation> constraintViolations) {
        Map<String, String> errorMessages = Maps.newHashMap();
        for (ConstraintViolation violation : constraintViolations) {
            errorMessages.put(violation.getPropertyPath().toString(), violation.getMessage());
        }
        return errorMessages;
    }

    /**
     * 辅助方法, 转换ConstraintViolationException中的Set<ConstraintViolations>为List<propertyPath message>.
     */
    public static List<String> extractPropertyAndMessageAsList(ConstraintViolationException e) {
        return extractPropertyAndMessageAsList(e.getConstraintViolations(), " ");
    }

    /**
     * 辅助方法, 转换Set<ConstraintViolations>为List<propertyPath message>.
     */
    public static List<String> extractPropertyAndMessageAsList(Set<? extends ConstraintViolation> constraintViolations) {
        return extractPropertyAndMessageAsList(constraintViolations, " ");
    }

    /**
     * 辅助方法, 转换ConstraintViolationException中的Set<ConstraintViolations>为List<propertyPath +separator+ message>.
     */
    public static List<String> extractPropertyAndMessageAsList(ConstraintViolationException e, String separator) {
        return extractPropertyAndMessageAsList(e.getConstraintViolations(), separator);
    }

    /**
     * 辅助方法, 转换Set<ConstraintViolation>为List<propertyPath +separator+ message>.
     */
    public static List<String> extractPropertyAndMessageAsList(Set<? extends ConstraintViolation> constraintViolations,
                                                               String separator) {
        List<String> errorMessages = Lists.newArrayList();
        for (ConstraintViolation violation : constraintViolations) {
            errorMessages.add(violation.getPropertyPath() + separator + violation.getMessage());
        }
        return errorMessages;
    }
}